﻿using DeviceDebugger.DataAccess;
using DeviceDebugger.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DeviceDebugger.View;
using GalaSoft.MvvmLight.Messaging;

namespace DeviceDebugger.ViewModel
{
    public class IndexViewModel : ViewModelBase
    {
        private ObservableCollection<StackerModel> _listStacker;
        /// <summary>
        /// 堆垛机列表
        /// </summary>
        public ObservableCollection<StackerModel> ListStacker
        {
            get { return _listStacker; }
            set { _listStacker = value; RaisePropertyChanged(() => ListStacker); }
        }
        private StackerModel _selectValue;
        public StackerModel SelectValue
        {
            get { return _selectValue; }
            set { _selectValue = value; RaisePropertyChanged(() => SelectValue); }
        }

        public RelayCommand ProtocolCommand => new RelayCommand(ProtocolCommand_Click);

        public RelayCommand AddCommand => new RelayCommand(AddCommand_Click);
        public RelayCommand EditCommand => new RelayCommand(EditCommand_Click);
        public RelayCommand DeleteCommand => new RelayCommand(DeleteCommand_Click);
        public RelayCommand RunCommand => new RelayCommand(RunCommand_Click);

        public IndexViewModel()
        {
            ListStacker = new(DataVisitor.GetStackers());

            //设置页面关闭后刷新堆垛机列表
            Messenger.Default.Register<string>(this, "SettingsReturn", (a) =>
            {
                ListStacker = new(DataVisitor.GetStackers());
            });
        }
        /// <summary>
        /// 打开通讯协议窗口
        /// </summary>
        private void ProtocolCommand_Click()
        {
            ProtocolView protocolView = new();
            protocolView.ShowDialog();
        }

        private void AddCommand_Click()
        {
            SettingWindows settings = new SettingWindows();
            settings.ShowDialog();
        }
        private void EditCommand_Click()
        {
            if (SelectValue != null)
            {
                SettingWindows settings = new SettingWindows();
                Messenger.Default.Send<StackerModel>(SelectValue, "Settings");
                settings.ShowDialog();

            }
        }
        private void DeleteCommand_Click()
        {
            if (SelectValue != null)
            {
                //先清理计划任务中的信息,再删除堆垛机中的信息
                if (DataVisitor.DeleteTask(SelectValue.Id))
                    if (DataVisitor.DeleteStacker(SelectValue.Id))
                        ListStacker.Remove(SelectValue);
            }
        }
        private void RunCommand_Click()
        {
            if (SelectValue != null)
            {
                Common.ApplicationStore.CurrentStacker = SelectValue;
                RunWindow runWindow = new();
                runWindow.DataContext = new RunWindowViewModel();
                Messenger.Default.Send<StackerModel>(SelectValue, "Running");
                runWindow.ShowDialog();
            }
        }
    }
}
